package org.example.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.example.domain.CarDictType;
import org.example.service.CarDictTpeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class CarDictTypeListener implements ReadListener<CarDictType> {

    private static final int BACH_COUNT=3;

    private List<CarDictType> cacheList = ListUtils.newArrayListWithExpectedSize(BACH_COUNT);
    @Autowired
    private CarDictTpeService carDictTpeService;

    public CarDictTypeListener(CarDictTpeService carDictTpeService) {
        this.carDictTpeService=carDictTpeService;
    }

    @Override
    public void invoke(CarDictType carDictType, AnalysisContext analysisContext) {
        System.out.println("解析出来的数据:"+carDictType);
        cacheList.add(carDictType);
        if(cacheList.size()>=BACH_COUNT){

            carDictTpeService.insertBach(cacheList);
            cacheList=ListUtils.newArrayListWithExpectedSize(BACH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        System.out.println("解析完成");
        if(cacheList.size()>=1){
            System.out.println("list:"+cacheList);
            carDictTpeService.insertBach(cacheList);
        }
    }
}
